package io.modelcontextprotocol.autoconfigure.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

public final class JsonParser {
    private static final ObjectMapper OBJECT_MAPPER;

    private JsonParser() {
    }

    public static ObjectMapper getObjectMapper() {
        return OBJECT_MAPPER;
    }

    public static <T> T fromJson(String json, Class<T> type) {
        Assert.notNull(json, "json cannot be null");
        Assert.notNull(type, "type cannot be null");

        try {
            return OBJECT_MAPPER.readValue(json, type);
        } catch (JsonProcessingException var3) {
            JsonProcessingException ex = var3;
            throw new IllegalStateException("Conversion from JSON to   failed"+type.getName(), ex);
        }
    }

    public static <T> T fromJson(String json, Type type) {
        Assert.notNull(json, "json cannot be null");
        Assert.notNull(type, "type cannot be null");

        try {
            return OBJECT_MAPPER.readValue(json, OBJECT_MAPPER.constructType(type));
        } catch (JsonProcessingException var3) {
            JsonProcessingException ex = var3;
            throw new IllegalStateException("Conversion from JSON to "+type.getTypeName()+" failed" , ex);
        }
    }

    public static <T> T fromJson(String json, TypeReference<T> type) {
        Assert.notNull(json, "json cannot be null");
        Assert.notNull(type, "type cannot be null");

        try {
            return OBJECT_MAPPER.readValue(json, type);
        } catch (JsonProcessingException var3) {
            JsonProcessingException ex = var3;
            throw new IllegalStateException("Conversion from JSON to "+type.getType().getTypeName()+" failed" , ex);
        }
    }

    public static String toJson(@Nullable Object object) {
        try {
            return OBJECT_MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException var2) {
            JsonProcessingException ex = var2;
            throw new IllegalStateException("Conversion from Object to JSON failed", ex);
        }
    }

    public static Object toTypedObject(Object value, Class<?> type) {
        Assert.notNull(value, "value cannot be null");
        Assert.notNull(type, "type cannot be null");
        Class<?> javaType = ClassUtils.resolvePrimitiveIfNecessary(type);
        if (javaType == String.class) {
            return value.toString();
        } else if (javaType == Byte.class) {
            return Byte.parseByte(value.toString());
        } else {
            BigDecimal bigDecimal;
            if (javaType == Integer.class) {
                bigDecimal = new BigDecimal(value.toString());
                return bigDecimal.intValueExact();
            } else if (javaType == Short.class) {
                return Short.parseShort(value.toString());
            } else if (javaType == Long.class) {
                bigDecimal = new BigDecimal(value.toString());
                return bigDecimal.longValueExact();
            } else if (javaType == Double.class) {
                return Double.parseDouble(value.toString());
            } else if (javaType == Float.class) {
                return Float.parseFloat(value.toString());
            } else if (javaType == Boolean.class) {
                return Boolean.parseBoolean(value.toString());
            } else if (javaType.isEnum()) {
                return Enum.valueOf( (Class<? extends Enum>) javaType, value.toString());
            } else {
                String json = toJson(value);
                return fromJson(json, javaType);
            }
        }
    }

    static {

        OBJECT_MAPPER = JsonMapper.builder()
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .disable(new SerializationFeature[]{SerializationFeature.FAIL_ON_EMPTY_BEANS})
                .addModules(JacksonUtils.instantiateAvailableModules()).build();
    }
}
